from django.shortcuts import render

# Create your views here.
from users.models import User
from rest_framework.response import Response
from rest_framework.views import APIView



# APIView

# GenericAPIView
# ListAPIView ,CreateAPIView

class RegisterUsernameCountAPIView(APIView):
    """
    获取用户名的个数
    GET:  /users/username/(?P<username>\w{5,20})/count/
    GET     /users/usernames/?username=xxx
    POST    /users/usernames/           username=xxxx

    """""
    def get(self,request,username):

        # 通过模型查询，获取用户名个数
        # 查询数据库 看有没有制定的数据
        count = User.objects.filter(username=username).count()
        # count = 0 不存在
        # count >= 1  存在
        # 组织数据
        context = {
            'count':count,
            'username':username     # 想返回就返回,不返回 它要数据再返回
        }
        return Response(context)


class RegisterPhoneCountAPIView(APIView):

    """
    查询手机号的个数
    GET: /users/phones/(?P<mobile>1[345789]\d{9})/count/
    """""
    def get(self, request,mobile):

        # 通过模型查询获取手机号个数
        count = User.objects.filter(mobile=mobile).count()

        # 组织数据
        context = {
            'count':count,
            'phone':mobile
        }

        return Response(context)
